#include "yesnobox.h"


#include <wiiuse/wpad.h>

#include <ogcsys.h>
#include <gccore.h>

YesNoBox::YesNoBox(Image* bg, Image* yesButton, Image* yesButton_over, Image* noButton, Image* noButton_over)
{

	bgSprite.SetPosition(0,0);
	bgSprite.SetImage(bg);
	
	yesSprite.SetPosition(80, 220);
	noSprite.SetPosition(380, 220);
	
	bgImg = bg;
	yesOver_img = yesButton_over;
	noOver_img  = noButton_over;
	yes_img     = yesButton;
	no_img      = noButton;
	
	yesSprite.SetImage(yes_img);
	noSprite.SetImage(no_img);
	
}

YesNoBox::~YesNoBox()
{

}

bool YesNoBox::GetYesNo(Pointer* ptr, GameWindow* gwd, Image* img_override)
{

	if(img_override != NULL)
	{
		bgSprite.SetImage(img_override);
	}
	else
	{
		bgSprite.SetImage(bgImg);
	}

	while(1)
	{
		bgSprite.Draw();
		
		WPAD_ScanPads();
		
		u16 btnsdown = WPAD_ButtonsDown(0);

        ir_t ir; // The struct for infrared
        
        WPAD_IR(WPAD_CHAN_0, &ir); // Let's get our infrared data

        ptr->SetPosition(ir.sx-200, ir.sy-250);
        ptr->SetRotation(ir.angle/2); // Set angle/2 to translate correctly
		
		if(btnsdown & WPAD_BUTTON_A)
		{
			if(yesSprite.GetImage() == yesOver_img)
			{
				return true;
			}
			else if(noSprite.GetImage() == noOver_img)
			{
				return false;
			}
		}
			
		if(btnsdown & WPAD_BUTTON_B || btnsdown & WPAD_BUTTON_HOME)
		{
			return false;
		}
		
		if(yesSprite.CollidesWith(ptr->GetSprite(), true))
		{
			yesSprite.SetImage(yesOver_img);
			noSprite.SetImage(no_img);
		}
		
		if(noSprite.CollidesWith(ptr->GetSprite(), true))
		{
			yesSprite.SetImage(yes_img);
			noSprite.SetImage(noOver_img);
		}
		
		yesSprite.Draw();
		noSprite.Draw();
		
		ptr->Render();
		
		gwd->Flush();
	}
}
